﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using RTGS.Library.Entities.Rtgs;
using RTGS.Library.DataAccess.Rtgs;
using System.Configuration;
using System.Data;
using RTGS.Library.Report;

namespace RTGS.Web.form.rtgs
{

public partial class form_rtgs_record_detail_o_form : BasePage
{
    private string id = string.Empty;
    private trntRepo trntrepo = new trntRepo(ConfigurationManager.ConnectionStrings["Rtgs"].ConnectionString);

    private stpRecordDetailORepo roleDal = new stpRecordDetailORepo(ConfigurationManager.ConnectionStrings["Rtgs"].ConnectionString);
    ReportSptDetailRecordO reportDal = new ReportSptDetailRecordO(ConfigurationManager.ConnectionStrings["Rtgs"].ConnectionString);

    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            DataTable dt = reportDal.GetMember();

            ddlDebitParticipant.Items.Clear();
            foreach (DataRow dr in dt.Rows)
            {
                ddlDebitParticipant.Items.Add(new ListItem(dr["memcde"].ToString().Trim(), dr["memcde"].ToString().Trim()));
            }

            ddlDebitParticipant.Items.Insert(0, new ListItem("---pilih data--", string.Empty));

            ddlCreditParticipant.Items.Clear();
            foreach (DataRow dr in dt.Rows)
            {
                ddlCreditParticipant.Items.Add(new ListItem(dr["memcde"].ToString().Trim(), dr["memcde"].ToString().Trim()));
            }

            ddlCreditParticipant.Items.Insert(0, new ListItem("---pilih data--", string.Empty));

            List<trnt> listtrnt = trntrepo.GetList();
            foreach (var item in listtrnt)
            {
                ddlTrn.Items.Add(new ListItem(item.trn, item.trndesc));
            }

            ddlTrn.Items.Insert(0, new ListItem("---Pilih Data---", string.Empty));

            if (Request.Params["Id"] != null)
            {
                id = HttpUtility.HtmlEncode(Convert.ToString(Request.Params["Id"]));
            }

            if (!Page.IsPostBack)
            {


                if (id.Trim().Length > 0) // Edit an existing item
                {
                    stpRecordDetailO obj = roleDal.GetById(id);
                    if (obj != null)
                    {
                        hfID.Value = id.ToString();

                        //GROUP TRN
                        //txtTrn.Text = obj.recordDetailOtrTrn;
                        //ddlDebitParticipant.Items.FindByValue(obj.recordDetailOtrTrn.Trim()).Selected = true;

                        if (obj.recordDetailOtrTrn != "")
                        {
                            if (ddlTrn.Items.Count > 0)
                            {
                                for (int i = 0; i < ddlTrn.Items.Count; i++)
                                {
                                    if (ddlTrn.Items[i].ToString().Trim() == obj.recordDetailOtrTrn.Trim())
                                    {

                                        ddlTrn.Items.FindByText(obj.recordDetailOtrTrn).Selected = true;
                                    }
                                }
                            }

                            trnt data = trntrepo.GetById(obj.recordDetailOtrTrn);

                            txtTrnDesc.Text = data.trndesc;
                        }

                        txtRelatedTrn.Text = obj.recordDetailOtrRelatedtrn;

                        //GROUP AMOUNT
                        if (obj.recordDetailOtrAmount != null && obj.recordDetailOtrAmount != "")
                            txtAmount.Text = Convert.ToDecimal(obj.recordDetailOtrAmount).ToString("#,###.00");
                        txtCurrency.Text = obj.recordDetailOstCurrency.Trim();
                        txtBussinesDay.Text = obj.recordDetailOtrValuedate.Trim();



                        //GROUP DEBIT PARTICIPANT
                        //txtDebitParticipant.Text = obj.recordDetailOstFromaccountnumber.Trim();
                        txtNameDebitParticipant.Text = obj.recordDetailOstFromaccountname.Trim();

                        //GROUP CREDIT PARTICIPANT
                        //txtCreditParticipant.Text = obj.recordDetailOstToaccountnumber.Trim();
                        txtNameCreditParticipant.Text = obj.recordDetailOstToaccountname.Trim();

                        //GROUP DEBIT PARTICIPANTS CLIENT
                        txtDebitParticipantsClient.Text = obj.recordDetailOstOriginatingname.Trim();
                        txtAccountDebitParticipantsClient.Text = obj.recordDetailOstOriginatingnumber.Trim();

                        //GROUP CREDIT PARTICIPANTS CLIENT
                        txtCreditParticipantsClient.Text = obj.recordDetailOstUltimatebenefname.Trim();
                        txtAccountParticipantsClient.Text = obj.recordDetailOstUltimatebenefaccount.Trim();

                        //GROUP DETAIL
                        txtRemitanceInformation.Text = obj.recordDetailOstPaymentdetails.Trim();
                        txtDetailOfCharges.Text = obj.recordDetailOtrRecordtype.Trim();
                        txtTransactionSubTypeCode.Text = obj.recordDetailOtrTransactioncode.Trim();
                        txtSender.Text = obj.recordDetailOstPaymentdetails.Trim();


                        if (obj.recordDetailOtrFrommember != null && obj.recordDetailOtrFrommember != "")
                            ddlDebitParticipant.Items.FindByValue(obj.recordDetailOtrFrommember.Trim()).Selected = true;
                        if (obj.recordDetailOtrTomember != null && obj.recordDetailOtrTomember != "")
                            ddlCreditParticipant.Items.FindByValue(obj.recordDetailOtrTomember.Trim()).Selected = true;

                        //txtMemberCodeCredit.Text = obj.recordDetailOtrTomember;
                        //txtMemberCodeDebit.Text = obj.recordDetailOtrFrommember;\
                        txtAccountDebitParticipant.Text = obj.recordDetailOstFromaccountnumber;
                        txtAccountCreditParticipant.Text = obj.recordDetailOstToaccountnumber;

                        txtReason.Text = obj.recordDetailOrecordRsncde;
                        txtPriority.Text = obj.recordDetailOrecordPriority;


                        //if (obj.recordDetailOrecordRsncde.Trim() == "001")
                        //    txtReason.Text = "001 : Value date is not valid";

                        //if (obj.recordDetailOrecordRsncde.Trim() == "002")
                        //    txtReason.Text = "002 : From/to member data is not valid!";

                        //if (obj.recordDetailOrecordRsncde.Trim() == "003")
                        //    txtReason.Text = "003 : Amount cannot is bigger than Amount Capping";

                        //if (obj.recordDetailOrecordRsncde.Trim() == "004")
                        //    txtReason.Text = "004 : Amount cannot is Null";



                        if (Request.Params["typeOutgoing"].ToString() == "repair")
                        {
                            txtReason.Font.Bold = true;
                            txtReason.ForeColor = System.Drawing.Color.Red;

                            if (obj.recordDetailOrecordRsncde.Trim() == "001")
                            {
                                txtBussinesDay.ReadOnly = false;
                                txtReason.Text = "001 : Value date is not valid";
                            }

                            if (obj.recordDetailOrecordRsncde.Trim() == "002")
                            {
                                ddlCreditParticipant.Enabled = true;
                                ddlDebitParticipant.Enabled = true;
                                txtReason.Text = "002 : From/to member data is not valid!";
                            }

                            if (obj.recordDetailOrecordRsncde.Trim() == "003")
                            {
                                txtAmount.Font.Bold = true;
                                txtAmount.ForeColor = System.Drawing.Color.Red;
                                txtReason.Text = "003 : Amount cannot is bigger than Amount Capping";
                            }

                            if (obj.recordDetailOrecordRsncde.Trim() == "004")
                            {
                                txtAmount.ReadOnly = false;
                                txtReason.Text = "004 : Amount cannot is Null";

                            }
                        }

                    }
                }
                else
                {
                    txtReason.Text = "000";
                    txtDetailOfCharges.Text = "1";
                    txtRelatedTrn.Text = "BOC" + DateTime.Now.ToString("yyyyMMddhhssf");
                    txtTransactionSubTypeCode.Text = "600";


                    txtAccountDebitParticipantsClient.ReadOnly = false;
                    txtAccountParticipantsClient.ReadOnly = false;
                    txtAmount.ReadOnly = false;
                    txtBussinesDay.ReadOnly = false;
                    txtCommentCharges.ReadOnly = false;
                    txtCreditParticipantsClient.ReadOnly = false;
                    txtCurrency.ReadOnly = false;
                    txtPriority.ReadOnly = false;
                    txtRemitanceInformation.ReadOnly = false;
                    txtSender.ReadOnly = false;
                    //txtTrn.ReadOnly = false;
                    txtDocumentStep.ReadOnly = false;
                    txtDebitParticipantsClient.ReadOnly = false;

                    ddlCreditParticipant.Enabled = true;
                    ddlDebitParticipant.Enabled = true;
                }

            }

        }
        catch (Exception ex)
        {

        }
    }
}

}